package org.webbase.report.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.webbase.common.JsonResult;
import org.webbase.common.util.JsonUtil;
import org.webbase.report.pojo.Form;
import org.webbase.report.pojo.FormColumn;
import org.webbase.report.pojo.FormParam;
import org.webbase.report.service.FormService;
import org.webbase.system.pojo.User;
import org.webbase.system.web.ModelAction;

@Controller
@RequestMapping(value="/report/form")
public class FormAction extends ModelAction<Form> {
	
	@Autowired private FormService formService;
	@Autowired private FormService formParamService;
	
	//跳到sql列表页
	@RequestMapping(method=RequestMethod.GET)
	public String list() {
		return "reports/reports/reports-list";
	}
	
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public String query(Form param) {
		DetachedCriteria criteria=getPaginator().getCriteria();
		if(!StringUtils.isBlank(param.getName())) {
			criteria.add(Restrictions.like("name", param.getName(),MatchMode.ANYWHERE));
		}
		formService.find(getPaginator());
		return pagin(getPaginator());
	}
	
	@RequestMapping(value = "/params", method=RequestMethod.GET)
	public String paramPage() {
		return "reports/reports/reports-params";
	}
	
	@RequestMapping(value = "/params", method = RequestMethod.POST)
	public void queryFormParam(HttpServletResponse response, @RequestParam String formId){
		printjson(response,formService.queryFormParams(formId));
	}
	
	@RequestMapping(value="/download")
	public void download(HttpServletResponse res, @RequestParam String formId, @RequestParam String params) {
		Map<String,Object> paramMap = JsonUtil.toObject(params, Map.class);
		
		String filePath = formService.createExcel(formId,paramMap);
		if(filePath==null) return;
		FileInputStream fis=null;
		try {
			File file=new File(filePath);
			fis = new FileInputStream(file);
			
			res.reset();
	        res.addHeader("Content-Disposition", "attachment;filename="+new String(file.getName().getBytes("gbk"),"iso-8859-1"));
	        res.addHeader("Content-Length", "" + file.length());
	        res.setContentType("application/octet-stream; charset=utf-8");
	        IOUtils.copy(fis, res.getOutputStream());
		} catch (Exception e) {
			throw new RuntimeException("下载文件出错!",e);
		} finally {
			if(fis!=null) {
				try {
					fis.close();
				} catch (IOException e) {}
			}
		}
	}
	
	@RequestMapping(value = "/add", method=RequestMethod.GET)
	public String addPage() {
		return "reports/reports/reports-add";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult add(@RequestBody Form form) {
		form.setCreateUserId(loginuser().getId());
		formService.add(form);
		return result(200, "OK");
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.GET)
	public String updatePage() {
		return "reports/reports/reports-update";
	}
	
	@RequestMapping(value="/update/{formId}",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult update(@PathVariable String formId, @RequestBody Form param) {
		
		Form form=formService.get(formId);
		form.setName(param.getName());
		form.setHsql(param.getHsql());
		form.setDescription(param.getDescription());
		form.setUseColumn(0);
		form.setCreateTime(form.getCreateTime());
		form.setCreateUserId(form.getCreateUserId());
		form.setStatus(form.getStatus());
		formService.update(form);
		return result(200, "OK");
	}
	
	@RequestMapping(value = "/delete", method=RequestMethod.GET)
	public String deletePage() {
		return "reports/reports/reports-delete";
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@RequestParam String formId) {
		formService.delete(formId);
		return result(200, "OK");
	}
	
	@RequestMapping(value = "/preview", method=RequestMethod.GET)
	public String previewPage() {
		return "reports/reports/reports-preview";
	}
	
	@RequestMapping(value = "/preview", method = RequestMethod.POST)
	public void preview(HttpServletResponse response, @RequestParam String formId, @RequestParam String params){
		printjson(response,new ArrayList<>());
	}

	@RequestMapping(value = "/getForm", method = RequestMethod.POST)
	@ResponseBody
	public Form getForm(@RequestParam String formId){
		Form form = formService.get(formId);
		form.setParamList(formService.queryFormParams(formId));
		form.setColumnList(formService.queryFormColumns(formId));
		return form;
	}
	
}
